/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package record;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
 *
 * @author mgeden
 */
public class WarningPrefer {
     private byte fajrEWM,sunRiseEWM,zuhrEWM,asrEWM,magribEWM,ishaEWM;
     private byte fajrWarn,sunRiseWarn,zuhrWarn,asrWarn,magribWarn,ishaWarn;
     private boolean isItFirstTime=false;
     public void setEarlyWarningMinutes (
             byte fajrEWM,
             byte sunRiseEWM,
             byte zuhrEWM, 
             byte asrEWM,
             byte magribEWM,
             byte ishaEWM)
     {
         this.fajrEWM= fajrEWM;
         this.sunRiseEWM=sunRiseEWM;
         this.zuhrEWM=zuhrEWM;
         this.asrEWM=asrEWM;
         this.magribEWM=magribEWM;
         this.ishaEWM=ishaEWM;

     }

   

    public void setWarningType (
            byte fajrWarn,
            byte sunRiseWarn,
            byte zuhrWarn, 
            byte asrWarn,
            byte magribWarn,
            byte ishaWarn)
     {
         this.fajrWarn= fajrWarn;
         this.sunRiseWarn=sunRiseWarn;
         this.zuhrWarn=zuhrWarn;
         this.asrWarn=asrWarn;
         this.magribWarn=magribWarn;
         this.ishaWarn=ishaWarn;
         this.isItFirstTime=false;

     }

    public byte getFajrEWM() {
        return fajrEWM;
    }
    public byte getSunRiseEWM() {
        return sunRiseEWM;
    }
    public byte getZuhrEWM() {
        return zuhrEWM;
    }
    public byte getAsrEWM() {
        return asrEWM;
    }
    public byte getMagribEWM() {
        return magribEWM;
    }

    public byte getIshaEWM() {
        return ishaEWM;
    }

    public byte getFajrWarn() {
        return fajrWarn;
    }

    public byte getSunRiseWarn() {
        return sunRiseWarn;
    }

    public byte getZuhrWarn() {
        return zuhrWarn;
    }

    public byte getAsrWarn() {
        return asrWarn;
    }

    public byte getMagribWarn() {
        return magribWarn;
    }

    public byte getIshaWarn() {
        return ishaWarn;
    }
   public boolean getIsItFirstTime() {
        return isItFirstTime;
    }
  public  byte[]  getWarningofAll() {
      loadWarnPreferInfo();  
      byte[]  warnings={fajrWarn,sunRiseWarn,zuhrWarn,asrWarn,magribWarn, ishaWarn};
      return  warnings;
    }
  public  byte[]  getEarlyWarningMinutesofAll() {
      loadWarnPreferInfo();  
      byte[]  earlyWarnings={fajrEWM,sunRiseEWM,zuhrEWM,asrEWM,magribEWM, ishaEWM};
      return  earlyWarnings;
    }

     
  public void saveWarnPreferInfo() {
                    RecordStore recordStore;
                    DataOutputStream outputStream;
                    try {
                        recordStore = RecordStore.openRecordStore("warningPrefer", true);
                    } catch (RecordStoreException rsc) {
                        return;
                    }
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    outputStream = new DataOutputStream(baos);
                    try {
                        outputStream.writeByte(fajrEWM);
                        outputStream.writeByte(sunRiseEWM);
                        outputStream.writeByte(zuhrEWM);
                        outputStream.writeByte(asrEWM);
                        outputStream.writeByte(magribEWM);
                        outputStream.writeByte(ishaEWM);
                        outputStream.writeByte(fajrWarn);
                        outputStream.writeByte(sunRiseWarn);
                        outputStream.writeByte(zuhrWarn);
                        outputStream.writeByte(asrWarn);
                        outputStream.writeByte(magribWarn);
                        outputStream.writeByte(ishaWarn);
                        
                        byte[] bytes = baos.toByteArray();
                        if (recordStore.getNumRecords() == 0)
                            recordStore.addRecord(bytes, 0, bytes.length);
                        else
                            recordStore.setRecord(1, bytes, 0, bytes.length);
                    } catch (Exception exception) {
                    } finally {
                        try {outputStream.close();} catch (Exception ex) {}
                        try {recordStore.closeRecordStore();} catch (Exception ex) {}
    }
	     }

 public void loadWarnPreferInfo() {
		         RecordStore recordStore = null;
		         DataInputStream inputStream = null;
		         try {
		 	    recordStore = RecordStore.openRecordStore("warningPrefer", false);
		             byte[] bytes = recordStore.getRecord(1);
		             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		             inputStream = new DataInputStream(bais);

		             fajrEWM=inputStream.readByte();
		             sunRiseEWM=inputStream.readByte();
		             zuhrEWM=inputStream.readByte();
		             asrEWM=inputStream.readByte();
		             magribEWM=inputStream.readByte();
		             ishaEWM=inputStream.readByte();
		             fajrWarn=inputStream.readByte();
		             sunRiseWarn=inputStream.readByte();
                             zuhrWarn=inputStream.readByte();
                             asrWarn=inputStream.readByte();
                             magribWarn=inputStream.readByte();
		             ishaWarn=inputStream.readByte();
                           

		         } catch (Exception ex) {
                             fajrEWM=0;
                             sunRiseEWM=45;
                             zuhrEWM=0;
                             asrEWM=0;
                             magribEWM=0;
                             ishaEWM=0;
                             fajrWarn=0;
                             sunRiseWarn=7;
                             zuhrWarn=5;
                             asrWarn=6;
                             magribWarn=3;
		             ishaWarn=4;
                             isItFirstTime=true;

		         } finally {
		             try {inputStream.close();} catch (Exception ex) {}
		             try {recordStore.closeRecordStore();} catch (Exception ex) {}
		         }
    }

}
